package com.itheima.reggin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggin.common.CustomException;
import com.itheima.reggin.entity.Category;
import com.itheima.reggin.entity.Dish;
import com.itheima.reggin.entity.Setmeal;
import com.itheima.reggin.mapper.CategroyMapper;
import com.itheima.reggin.service.CategoryService;
import com.itheima.reggin.service.DishService;
import com.itheima.reggin.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategroyMapper, Category> implements CategoryService {

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;
    @Autowired
    CategroyMapper categroyMapper;
    /**
     * 删除菜品   但是之前需要判断
     */
    @Override
    public void remove( Long id){
        LambdaQueryWrapper<Dish> dishQuery=new LambdaQueryWrapper();
        dishQuery.eq(Dish::getCategoryId,id);
        int count = dishService.count(dishQuery);
        if(count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除！");
        }
        LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<>();
        setmealQuery.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setmealQuery);
        if(count1>0){
            throw new CustomException("当前分类下关联了套餐，不能删除！");
        }
        categroyMapper.deleteById(id);
    }
}
